<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="任务名" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学情分析" prop="learnAnalyse">
        <el-input
          v-model="queryParams.learnAnalyse"
          placeholder="请输入学情分析"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价指标" prop="reviewIndex">
        <el-input
          v-model="queryParams.reviewIndex"
          placeholder="请输入评价指标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:list:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:list:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:list:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:list:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="listList" height="500px" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务名" align="center" prop="taskName" />
      <el-table-column label="学情分析" align="center" prop="learnAnalyse" />
      <el-table-column label="评价指标" align="center" prop="reviewIndex" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:list:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:list:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名" />
        </el-form-item>
        <!-- <el-form-item label="学情分析" prop="learnAnalyse">
          <template slot-scope="">
            <el-tag v-for="dict in dict.type.learn_analyse" :key="dict.value">{{ dict.label }}</el-tag>
          </template>
        </el-form-item> -->
        <el-form-item label="评价指标" prop="reviewIndex">
          <!-- <el-input v-model="form.reviewIndex" placeholder="请输入评价指标" /> -->
          <el-tag v-for="(dict, dictIndex) in dict.type.learn_analyse" :key="dict.value">
            {{ dict.label }}
            <div>
              <div
                v-for="(dict, key) in 5"
                :key="dict.value"
                class="subject_score"
              >
                <span>{{ dict.label }}</span>
                <el-input
                  v-model="learnAnalyseObj[`key${dictIndex}`][key]"
                  :placeholder="`请输入评价指标名称${key+1}`"
                  style="width: 90%;"
                />
              </div>
            </div>
          </el-tag>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { taskList, getList, delList, addList, updateList } from '@/api/system/taskList'
import { Message } from 'element-ui'

export default {
  name: 'TaskList',
  dicts: ['learn_analyse'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      listList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        learnAnalyse: null,
        reviewIndex: null,
        other1: null,
        other2: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      optType: 'add',
      learnAnalyseObj: {
        key0: [],
        key1: [],
        key2: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true
      taskList(this.queryParams).then(response => {
        this.listList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        learnAnalyse: null,
        reviewIndex: null,
        remark: null,
        other1: null,
        other2: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加任务'
      Object.keys(this.learnAnalyseObj).forEach(el => {
        this.learnAnalyseObj[el] = []
      })
      this.optType = 'add'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.optType = 'edit'
      const id = row.id || this.ids
      getList(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改任务'
        if (row.reviewIndex) {
          row.reviewIndex.split(';').forEach((el, key) => {
            this.learnAnalyseObj[`key${key}`] = el.split(',')
          })
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.form.taskName) {
            Message.error('任务名不能为空')
            return
          }
          let str = ''
          let num = 0
          Object.keys(this.learnAnalyseObj).forEach((el, key) => {
            str = str + this.learnAnalyseObj[el].join(',') + (key < 2 ? ';' : '')
            num += this.learnAnalyseObj[el].length
          })

          if (num !== 15) {
            Message.error('所有评价指标名称必填！')
            return
          }
          this.form.reviewIndex = str
          this.form.learnAnalyse = this.dict.type.learn_analyse.map(el => el.label).join(',')

          if (this.form.id !== null) {
            updateList(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addList(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
        return delList(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/list/export', {
        ...this.queryParams
      }, `list_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style rel="stylesheet/scss" scoped lang="scss">

.subject_score {
  display: flex;
  margin-bottom: 5px;
   .item {
    width: 200px;
    color: #606266;
    font-weight: 700;
   }
}

::v-deep .el-tag {
  margin-right: 10px;
}
</style>

